/*
 * @Description:
 * @Author: Zzt
 * @Date: 2022-12-21 11:34:50
 * @LastEditTime: 2023-01-03 16:46:55
 */
import {
  SET_SELECT_UNIT,
  SET_SELECTED_ITEM,
  DELETE_BUSINESS_ITEM,
  GET_BUSINESS,
  EDIT_BUSINESS,
  GET_ENCLOSURE,
  ADD_GUIDANCE,
  ADD_ANNUAL,
  SAVE_ANNEX,
  DELECT_ANNUAL,
  CLEAR_ANNUAL
} from '../constants';
import {
  delItemByIdArray,
  insertItem,
  editItemById,
  updateItemById,
  delItemById
} from '@/utils/list';

export const BusinessGuidanceReducer = (state, action) => {
  switch (action.type) {
    /**
     * @description: 设置选中的单位
     * @param {Obj} data 选中的单位Object
     */
    case SET_SELECT_UNIT: {
      return {
        ...state,
        selectUnit: action.data
      };
    }
    /* 新增列表 */
    case ADD_GUIDANCE: {
      return { ...state, businessList: insertItem(action.data, state.businessList) };
    }

    /**
     * @description: 获取列表
     */
    case GET_BUSINESS: {
      return { ...state, businessList: action.data };
    }

    case DELETE_BUSINESS_ITEM: {
      return {
        ...state,
        businessList: delItemByIdArray(action.ids, state.businessList)
      };
    }
    /**
     * @description: 修改检测任务
     */
    case EDIT_BUSINESS:
      return {
        ...state,
        businessList: editItemById(action.id, action.data, state.businessList)
      };
    /**
     * @description: 保存表格选中的条目id
     * @param {number[]} data
     */
    case SET_SELECTED_ITEM:
      return { ...state, selectItems: action.data };
    /* 获取选中列表 */
    case GET_ENCLOSURE:
      return { ...state, enclosure: action.data, enclosureList: action.list };
    /*  */
    case SAVE_ANNEX: {
      return { ...state, enclosureList: insertItem(action.data, state.enclosureList) };
    }
    /* 山一项 */
    case DELECT_ANNUAL:
      return {
        ...state,
        enclosureList: delItemById(action.id, state.enclosureList, `fileNo`)
      };
    case CLEAR_ANNUAL:
      return {
        ...state,
        enclosureList: []
      };
    default:
      console.log('untouched', action);
      return state;
  }
};
